<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Request;
class Index extends Controller
{
    public function index()
    {	
    	if(!cookie("name")){
    		$this->redirect("index/login");
    	}
       return $this->fetch();
    }
    public function login(){
    /*	$user=db("tk_user");
    	dump($user->select());*/
    	if(request()->isPost()){
    		$uname=input("post.uname");
    		$pwd=input("post.pwd");
    		$conpwd=md5(input("post.pwd"));
    		$user=model("TkUser");
    		$where=[
    			'uname'=>$uname,
    			'pwd'=>$conpwd
    		];
    		$ifcheck=input("post.ifcheck");
    		$num=$user->where($where)->count();
        $this->assign("num",$num);
        $result=$user->where($where)->select();
        $status=[];
        foreach ($result as $k=>$v) {
          $status[]=$v->toArray();
        }
        $mess=[];
        foreach ($status as $k=>$v) {
          $mess=$v['statu'];
        }
        if($mess==0){
          $this->error("该用户已被禁用,请联系管理员","index/login");
        }else{
        		if($num==1){
        			cookie("name",$uname);
        			if($ifcheck!=false){
        				cookie("uname",$uname);
        				cookie("repwd",$pwd);

        			}
              $time=time();
              $uname=input("post.uname");
              //获取最后一次登录时间和ip
              $time=date("Y-m-d H:i:s",$time);
              $ip=request()->ip();
              $model=model('TkUser');
              $model->save(['lastlogintime'=>$time,'lastloginip'=>$ip],['uname'=>$uname]);
        			$this->success("登陆成功","index/index");
        		}else{
        			$this->error("登陆失败","index/login");
        		}
        	}
         }
    	if(input("a")==1){
    		cookie("name",null);
    	}
      
    	return $this->fetch();
    }
    
}
